<?php
/* Common */
require '../common.php';
require '../database.php';
require 'db_data_loader.php';
db_connect();
$subdivisions = array_keys(get_subdivisios_associative());
$support_measures = array_keys(get_subdivisios_measures_associative());

/* Configurations */
if (count($_POST)) {
    $n_measures = $_POST['n_measures'];
    $n_subdivisions = $_POST['n_subdivisions'];
    $date = $_POST['date'];
    $n_time = $_POST['n_time'];
} else {
    $n_measures = count($support_measures);
    $n_subdivisions = count($subdivisions);
    $date = '2008-01-01';
    $n_time = 3;
}

?>
<form action="#" method="post">
    Measure count: <input name="n_measures" value="<?=$n_measures;?>"/><br/>
    Subdivision count: <input name="n_subdivisions" value="<?=$n_subdivisions;?>"/><br/>
    Max time for 1 application: <input name="n_time" value="<?=$n_time;?>"/><br/>
    Date: <input name="date" value="<?=$date;?>"/><br/>
    <input name="drop" id="drop" type="checkbox" value="yes" checked="checked"/><label for="drop">Remove old data</label><br/>
    <input type="Submit"/>
</form>

<?
if (count($_POST)) {
    /* Generating data */
    $drop_old = $_POST['drop'];
    $first = true;
    $sql = "-- support_administration --\n\n";
    $sql .= "INSERT INTO support_administration (measure_code, subdivision_code, time, `date`) VALUES\n";
    $data = mysql_escape_string($date);
    for ($i_measure = 0; $i_measure < $n_measures; $i_measure++) {
        for ($i_subdivision = 0; $i_subdivision < $n_subdivisions; $i_subdivision++) {
            if ($first) {
                $first = false;
            } else {
                $sql .= ",\n";
            }
            $subdivision = mysql_escape_string($subdivisions[$i_subdivision]);
            $measure = mysql_escape_string($support_measures[$i_measure]);
            $time = rand(0, $n_time);
            $sql .= "('$measure', '$subdivision', $time, '$date')";
        }
        $sql .= "\n";
    }
    $sql .= ';';
    
    /* Saving to database and output */
    if ($drop_old) {
        mysql_query("TRUNCATE support_administration;");
    }
    mysql_query($sql);
    echo 'Saved to DB';
    echo '<pre>' . htmlspecialchars($sql) . '</pre>';
}

db_close();
?>
<a href="../">Pagrindinis</a>